package net.searchdata.pre;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import java.util.Arrays;
import java.util.List;

import org.junit.Test;

public class Entry25 {
	@Test
	public void test1() {
		List list = Arrays.asList(1, 2, 3);
		Integer value = (Integer) reduce(list, 0, new Function() {
			@Override
			public Object apply(Object o1, Object o2) {
				return ((Integer) o1).intValue() + ((Integer) o2).intValue();
			}
		});

		assertThat(value, is(6));
	}

	private Object reduce(List list, Object initValue, Function function) {
		synchronized (list) {
			Object result = initValue;
			for (Object o : list) {
				result = function.apply(result, o);
			}

			return result;
		}
	}
}